package com.szholly.pro.watergas_server.ps.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.szholly.data.general.*;
import com.szholly.plug.form.excel.utils.StringUtil;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.utils.spring.SpringBeanFactory;
import org.springframework.stereotype.Service;

/**
 * 超标比对
 *
 */
@Service
public class ExceedComparisonService {

	/**
	 * 获取超标比对记录
	 * @param params
	 * @param pageInfo
	 * @return
	 * @throws Exception
	 */
	public List<RowBase> getExceedComparisonList(Map<String, String> params, PageInfo pageInfo) throws Exception {
		List<GlobalParameter> listParam = new ArrayList<GlobalParameter>();
		String sql = " select t1.*, t3.name as WWTP_NAME,t4.name as STATION_NAME, t4.flow_type, t5.name DEVICE_ALIAS_NAME, t6.c_userealname as MEASURE_PERSON_NAME, "
						+ "  d1.S_DOMAINCAPTION as MEASURE_ITEM_NAME, d2.S_DOMAINCAPTION as FLOW_TYPE_NAME, d3.S_DOMAINCAPTION as MEASURE_CONCLUSION_NAME  "
						+ " from OP_EXCEED_COMP t1 "
						+ " left join OPERATION_TASK t2 on t1.TASK_ID = t2.TASK_CODE "
						+ " left join BC_ENTERPRISE_ATTR t3 on t2.WWTP_ID = t3.EXP_NO "
						+ " left join BC_SITE_INFO t4 on t2.STATION_ID = t4.EXP_NO "
						+ " left join BC_SITE_DEVICE_RPT  t5 on t1.DEVICE_NUMBER = t5.device_num "
						+ " left join safe_user t6 on t1.MEASURE_PERSON = t6.s_userid "
						+ " left join DATA_DEFULATDOMAIN d1 on ( d1.S_DOMAINNAME = 'MONITOR_PARAM' AND d1.S_DOMAINCODE = t1.MEASURE_ITEM ) "
						+ " left join DATA_DEFULATDOMAIN d2 on ( d2.S_DOMAINNAME = 'STATION_FLOW_TYPE' AND d2.S_DOMAINCODE = t4.FLOW_TYPE ) "
						+ " left join DATA_DEFULATDOMAIN d3 on ( d3.S_DOMAINNAME = 'OPERATION_COMPARISON_CONCLUSION' AND d3.S_DOMAINCODE = t1.MEASURE_CONCLUSION ) "
						+ " where 1=1";

		if (params.containsKey("task_code")) {
			sql += " and t1.TASK_ID = @task_code";
			listParam.add(new GlobalParameter("task_code", params.get("task_code")));
		} else {
			if (params.containsKey("wwtp_id")) {
				sql += " and t4.WWTP_ID = @wwtp_id";
				listParam.add(new GlobalParameter("wwtp_id", params.get("wwtp_id")));
			}

			if (params.containsKey("flow_type")) {
				sql += " and t4.FLOW_TYPE = @flow_type";
				listParam.add(new GlobalParameter("flow_type", params.get("flow_type")));
			}
			
			if (params.containsKey("water_type")) {
				sql += " and t4.STATION_CATEGORY = @water_type";
				listParam.add(new GlobalParameter("water_type", params.get("water_type")));
			}

			if (params.containsKey("station_id")) {
				sql += " and t2.STATION_ID = @station_id";
				listParam.add(new GlobalParameter("station_id", params.get("station_id")));
			}
			
			if (params.containsKey("measure_items")) {
				sql += " and t1.MEASURE_ITEM in ("+params.get("measure_items")+")";
			}
			
			if (params.containsKey("measure_person")) {
				sql += " and t1.MEASURE_PERSON = @measure_person";
				listParam.add(new GlobalParameter("measure_person", params.get("measure_person")));
			}
			
			if (params.containsKey("measure_conclusion")) {
				sql += " and t1.MEASURE_CONCLUSION = @measure_conclusion";
				listParam.add(new GlobalParameter("measure_conclusion", params.get("measure_conclusion")));
			}

			if (params.containsKey("start")) {
				sql += " and t1.MEASURE_START_TIME >= to_date('" + params.get("start")
						+ " 00:00:00', 'YYYY-MM-DD HH24:MI:SS')";
			}
			if (params.containsKey("end")) {
				sql += " and t1.MEASURE_START_TIME <= to_date('" + params.get("end") + " 23:59:59', 'YYYY-MM-DD HH24:MI:SS')";
			}
		}

		return DataUtil.getPageRow(sql, listParam, null, pageInfo);
	}
	
	
	/**
	 * 获取超标比对任务数据记录列表
	 * 
	 * @param params
	 * @return
	 */
	public List<RowBase> getExceedComparisonDataList(Map<String, String> params) {
		if (params == null)
			return null;

		String strSTATION_ID = params.get("STATION_ID");
		String strPARAM_ID = params.get("PARAM_ID");
		String strJOB_ID = params.get("JOB_ID");
		String strSTART_TIME = params.get("START_TIME");
		String strEND_TIME = params.get("END_TIME");
		if (StringUtil.IsNullOrEmpty(strSTATION_ID)
				|| StringUtil.IsNullOrEmpty(strPARAM_ID)
				|| StringUtil.IsNullOrEmpty(strJOB_ID))
			return null;

		String strSQL = " select t.TASK_CODE, t.TASK_STATUS, t.START_TIME as TASK_START_TIME, t.END_TIME as TASK_END_TIME, t.FINISH_TIME as TASK_FINISH_TIME,  \n"
				+ " t1.*, \n" + " t2.NAME as STATION_NAME, \n" + " t3.GROUP_NAME as GROUP_NAME, \n"
				+ " t4.C_USEREALNAME as USER_NAME \n" + " from OP_TASK t \n"
				+ " inner join OP_EXCEED_COMP t1 ON t1.TASK_ID = t.TASK_CODE \n" // dif
				+ " left join BC_SITE_INFO t2 on t2.EXP_NO = t.STATION_ID \n"
				+ " left join OC_GROUP t3 on t3.GUID = t.OPERATION_GROUP_ID \n"
				+ " left join SAFE_USER t4 on t4.S_USERID = t.FINISH_BY \n" + " where t.STATION_ID = '" + strSTATION_ID
				+ "' \n" + " AND t.JOB_ID = '" + strJOB_ID + "' \n" + " AND t1.MEASURE_ITEM = '" + strPARAM_ID + "' \n" // dif
				+ " AND t.START_TIME >= to_date('" + strSTART_TIME + " 00:00:00', 'yyyy-mm-dd hh24:mi:ss') \n"
				+ " AND t.START_TIME <= to_date('" + strEND_TIME + " 00:00:00', 'yyyy-mm-dd hh24:mi:ss') \n"
				+ " order by t.START_TIME desc ";

		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		List<RowBase> rcdList = DataUtil.getSelectRows(strSQL, null, pTable);

		return rcdList;
	}
}
